package cn.icanci.mv.dao.impl;


import cn.icanci.mv.dao.ICustomerDao;
import cn.icanci.mv.entity.Customer;
import cn.icanci.mv.entity.CustomerDTO;
import cn.icanci.mv.entity.Order;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: icanci
 * @ProjectName: kangaroo-orm
 * @PackageName: cn.icanci.mv.entity.dao.impl
 * @Date: Created in 2020/8/31 14:44
 * @ClassAction: ICustomerDao 顶级接口的实现类
 */
public class CustomerDaoImpl implements ICustomerDao {
    private static final String URL = "jdbc:mysql:///j08?serverTimezone=UTC";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "root";
    private static Connection connection;
    private static PreparedStatement preparedStatement;
    private static ResultSet resultSet;
    private static OrderDaoImpl orderDaoImpl = new OrderDaoImpl();

    @Override
    public List<Customer> findAll() {
        List<Customer> customers = new ArrayList<>();
        try {
            init();
            // 先查询自己
            String selectCusSql = "select id,cname,phone from tbl_customer";
            preparedStatement = connection.prepareStatement(selectCusSql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Customer customer = new Customer();
                customer.setId(resultSet.getInt(1));
                customer.setCname(resultSet.getString(2));
                customer.setPhone(resultSet.getString(3));
                // 查找自己相关联的
                List<Order> allByCustomerId = orderDaoImpl.findAllByCustomerId(resultSet.getInt(1));
                customer.setOrders(allByCustomerId);
                customers.add(customer);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResources();
        }
        return customers;
    }

    @Override
    public void deleteById(Integer id) {
        try {
            init();
            // 删除自己
            String deleteCusSql = "delete from tbl_customer where id = ?";
            preparedStatement = connection.prepareStatement(deleteCusSql);
            preparedStatement.setInt(1, id);
            preparedStatement.execute();
            // 删除和自己相关联的
            orderDaoImpl.deleteByCustomerId(id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResources();
        }
    }

    @Override
    public List<CustomerDTO> findOrder(Integer id) {
        List<CustomerDTO> list = new ArrayList<>();
        try {
            init();
            // 查找自己
            String selectCusSql = "select id,cname  from tbl_customer where id = ?";
            preparedStatement = connection.prepareStatement(selectCusSql);
            preparedStatement.setInt(1, id);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                CustomerDTO customerDTO = new CustomerDTO();
                customerDTO.setId(resultSet.getInt(1));
                customerDTO.setCname(resultSet.getString(2));
                // 查询和自己相关联的
                List<Order> allByCustomerId = orderDaoImpl.findAllByCustomerId(resultSet.getInt(1));
                customerDTO.setOrders(allByCustomerId);
                list.add(customerDTO);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResources();
        }
        return list;
    }

    @Override
    public void insert(Customer customer) {
        try {
            init();
            // 插入自己的数据
            String sql = "insert into tbl_customer(cname,phone) values(?,?) ";
            preparedStatement = connection.prepareStatement(sql);
            CustomerDaoImpl.preparedStatement.setString(1, customer.getCname());
            CustomerDaoImpl.preparedStatement.setString(2, customer.getPhone());
            CustomerDaoImpl.preparedStatement.executeUpdate();
            // 插入关联数据
            if (null != customer.getOrders()) {
                for (Order order : customer.getOrders()) {
                    // System.out.println(orderDaoImpl);
                    orderDaoImpl.insertOrder(order, customer.getId());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResources();
        }
    }

    @Override
    public Customer findById(Integer id) {
        Customer customer = new Customer();
        try {
            init();
            // 查询自己的数据
            String sql = "select id,cname,phone from tbl_customer where id = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                customer.setId(resultSet.getInt(1));
                customer.setCname(resultSet.getString(2));
                customer.setPhone(resultSet.getString(3));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResources();
        }
        return customer;
    }

    private static void init() {
        try {
            // 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 连接数据库
            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            orderDaoImpl = new OrderDaoImpl();
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }

    private static void closeResources() {
        try {
            if (connection != null) {
                connection.close();
            }
            if (preparedStatement != null) {
                preparedStatement.close();
            }
            if (resultSet != null) {
                resultSet.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
